class AulasController < ApplicationController
  # GET /aulas
  # GET /aulas.xml
  def index
    @aulas = Aula.find(:all, :order => 'data desc')

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @aulas }
    end
  end

  # GET /aulas/1
  # GET /aulas/1.xml
  def show
    @aula = Aula.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @aula }
    end
  end

  # GET /aulas/new
  # GET /aulas/new.xml
  def new
    @aula = Aula.new
    @presentes = []
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @aula }
    end
  end

  # GET /aulas/1/edit
  def edit
    @aula = Aula.find(params[:id])
    get_presentes
  end

  # POST /aulas
  # POST /aulas.xml
  def create
    @aula = Aula.new(params[:aula])

    usuario = session["usuario"]
    voluntario = Voluntario.find(:first, :conditions => {:usuario_id => usuario.id})

    if not voluntario.nil?
        @aula.voluntario = voluntario
    end

    respond_to do |format|
      if @aula.save
        set_presentes
        flash[:notice] = 'Aula was successfully created.'
        format.html { redirect_to(@aula) }
        format.xml  { render :xml => @aula, :status => :created, :location => @aula }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @aula.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /aulas/1
  # PUT /aulas/1.xml
  def update
    @aula = Aula.find(params[:id])

    respond_to do |format|
      if @aula.update_attributes(params[:aula])
        set_presentes
        flash[:notice] = 'Aula was successfully updated.'
        format.html { redirect_to(@aula) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @aula.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /aulas/1
  # DELETE /aulas/1.xml
  def destroy
    @aula = Aula.find(params[:id])
    @aula.destroy

    respond_to do |format|
      format.html { redirect_to(aulas_url) }
      format.xml  { head :ok }
    end
  end

  private 
        def set_presentes
            aula_alunos = AulaAlunos.find(:all, :conditions => {:aula_id => @aula.id})
            aula_alunos.each do |a|
                a.destroy
            end
            unless params[:presentes].nil?
                params[:presentes].each do |p|
                    aula_aluno = AulaAlunos.new
                    aula_aluno.aula = @aula
                    aula_aluno.aluno_id = p
                    aula_aluno.save
                end
            end
        end

        def get_presentes
            @presentes = []
            aula_alunos = AulaAlunos.find(:all, :conditions => {:aula_id => @aula.id})
            aula_alunos.each do |a|
                aluno = a.aluno
                @presentes << {:id => aluno.id, :nome => aluno.nome}
            end
            puts "\n\n\n"
            puts @presentes
            puts "\n\n\n"
        end

end
